
function answerList(options,answerArr){
    let newList = [];
    let status = 0;
    let marks = ['A','B','C','D'];
    options.forEach((item,index) => {
        if(item){
            let icon = marks[index];
            let isRightAnswer = 0;
            if(answerArr.includes(icon)){
                isRightAnswer = 1;
            }
            let row = {
                'icon': icon,
                'title': item,
                'isRightAnswer':isRightAnswer,
                'mark':0,
            };
            newList.push(row);
        }
    })
    return {status:status, list:newList}
}
// 回答问题
function resolveAnswer(list,resolve,answerArr){
    let isWrong = 0;
    let status = 0;
    list.forEach((item,index) => {
        if(item.icon == resolve){
            if(item.isRightAnswer === 1){
                item.mark = 1;
            }else{
                item.mark = 2;
                isWrong = 1;
            }
        }
    })
    if(isWrong === 0){
        let rightNum = 0;
        list.forEach((item,index) => {
            if(item.mark == 1){
                rightNum++;
            }
        })
        if(rightNum == answerArr.length){
            status = 1;
        }
    }else{
        status = 2;
    }

    return {status:status, list:list}
}
//错题收集,action:add添加，clear清除
async function wrongList(item,action){
    let cacheKey = "wrongList";
    if(action == 'add'){
        let list = await $xt.cget(cacheKey);
        if(!list){
            list = {}
        }
        if(!list[item.question_id]){
            list[item.question_id] = item;
            await $xt.cset(cacheKey,list);
        }
    }   
}
// 操作错误正确以及收藏的数量
async function actNum(type,category,action){
    let cacheKey = type+'_'+category+"_num";
    let num = await $xt.cget(cacheKey);
    if(!num) num = 0;
    if(action == "add"){
        num ++;
        $xt.cset(cacheKey,num);
    }else if(action == 'get'){
        return num;
    }
}
// 更改|获取 index和page
async function indexAndPage(prev,indexOrPage,action,value){
    let cacheKey = prev+'_'+indexOrPage;
    if(action == 'get'){
        let res = await $xt.cget(cacheKey);
        if(!res){
            res = indexOrPage === 'index'? 0 : 1;
        }
        return res;
    }else if(action === 'edit'){
        $xt.cset(cacheKey,value);
    }
}
//收藏
async function collect(item,action){
    let cacheKey = "collectList";
    let list = await $xt.cget(cacheKey);
    if(!list || list.length == 0){
        list = {}
    }
    let collectStatus = 0;
    if(action == 'add'){
        if(!list[item.question_id]){
            //收藏
            list[item.question_id] = item;
            collectStatus = 1;
        }else{
            // 取消收藏
            delete list[item.question_id]
        }
        await $xt.cset(cacheKey,list);
        return {status:collectStatus,num:Object.keys(list).length};
    }else if(action == 'num'){
        if(list[item.question_id]){
            collectStatus = 1;
        }else{
            collectStatus = 0;
        }
        return {status:collectStatus,num:Object.keys(list).length};
    } 
}

export default {
    answerList,resolveAnswer,wrongList,actNum,indexAndPage,collect
}
